package pers.cz.commons.eltree;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: elTree
 * @author: Cheng Zhi
 * @create: 2022-01-16 20:08
 **/
public class ElTree {

    public static List<TreeNode> treeNodeList;

    public ElTree() {
        //TreeNode root = new TreeNode();
        treeNodeList = new ArrayList<TreeNode>();
        //treeNodeList.add(root);
    }

    public void insert(Integer id, String name, Integer parentId) {

        TreeNode treeNode = new TreeNode();
        treeNode.setId(id);
        treeNode.setName(name);
        treeNode.setParentId(parentId);

        // 找父节点
        for (TreeNode rootTreeNode : treeNodeList) {
            // 先遍历链表，找到父节点
            TreeNode parentTreeNode = searchParentNode(parentId,rootTreeNode);
            if (parentTreeNode != null) {
                // 挂靠到父节点下
                // 如果父节点还没有子节点，则添加一个
                if (parentTreeNode.getNextNodeList() == null || parentTreeNode.getNextNodeList().size() == 0) {
                    List<TreeNode> childrenLit = new ArrayList<>();
                    childrenLit.add(treeNode);
                    parentTreeNode.setNextNodeList(childrenLit);
                } else {
                    parentTreeNode.getNextNodeList().add(treeNode);
                }

                //return;
            }
        }

        List<TreeNode> childrenNodes = new ArrayList<>();
        // 找子节点
        for (Iterator<TreeNode> iterator = treeNodeList.iterator(); iterator.hasNext();) {
            TreeNode rootTreeNode = iterator.next();
            TreeNode childrenNode = searchChildrenNode(id, rootTreeNode);
            if (childrenNode != null) {
                // 将已有的节点挂靠到新节点上，然后删除已有的节点
                childrenNodes.add(childrenNode);
                // 删除
                iterator.remove();
            }
        }
        if (childrenNodes.size() > 0) {
            treeNode.setNextNodeList(childrenNodes);
        }

        // 重新整理树节点

        this.treeNodeList.add(treeNode);


    }

    /**
     * 寻找子节点
     * @param parentId
     * @param node
     * @return
     */
    private TreeNode searchChildrenNode(Integer parentId, TreeNode node) {

        if (parentId == null) {
            return null;
        }

        if (node.parentId == parentId) {
            return node;
        }

        return null;


    }
    /**
     * 寻找父节点
     * @param id
     * @return
     */
    private TreeNode searchParentNode(Integer id, TreeNode node) {

        if (id == null) {
            return null;
        }
        if (node.getId() == null) {
            return null;
        }

        if (node.getId() == id) {
            return node;
        }
        if (node.getNextNodeList() != null && node.getNextNodeList().size() > 0) {
            for (TreeNode treeNode : node.getNextNodeList()) {

                if (treeNode.getId() == id) {
                    return treeNode;
                } else {
                    TreeNode treeNodeResult = searchParentNode(id, treeNode);
                    if (treeNodeResult != null) {
                        return treeNodeResult;
                    }

                }
            }
        }

        return null;
    }

    public static void main(String[] args) {
        ElTree elTree = new ElTree();

        elTree.insert(1,"管理员", null);
        System.out.println(1);
        elTree.insert(2,"测试经理", 4);
        System.out.println(2);
        elTree.insert(3,"开发经理", 4);
        System.out.println(3);
        elTree.insert(4,"经理", 5);
        System.out.println(4);
        elTree.insert(5,"总监", 1);
        System.out.println(5);
        elTree.insert(6,"开发组长", 3);
        System.out.println(6);
        elTree.insert(7,"测试组长", 2);
        System.out.println(7);


        Gson gson = new Gson();
        System.out.println(gson.toJson(elTree.treeNodeList));
    }

    public static class TreeNode {

        Integer parentId;
        // 节点id
        Integer id;
        // 节点名称
        String name;
        // 前继节点
        TreeNode preNode;
        // 后继节点应该是个list
        List<TreeNode> nextNodeList;
        // 后继节点
        TreeNode nextNode;
        // 是否存在兄弟节点
        boolean hasBrother;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public TreeNode getPreNode() {
            return preNode;
        }

        public void setPreNode(TreeNode preNode) {
            this.preNode = preNode;
        }

        public List<TreeNode> getNextNodeList() {
            return nextNodeList;
        }

        public void setNextNodeList(List<TreeNode> nextNodeList) {
            this.nextNodeList = nextNodeList;
        }

        public TreeNode getNextNode() {
            return nextNode;
        }

        public void setNextNode(TreeNode nextNode) {
            this.nextNode = nextNode;
        }

        public boolean isHasBrother() {
            return hasBrother;
        }

        public void setHasBrother(boolean hasBrother) {
            this.hasBrother = hasBrother;
        }

        public Integer getParentId() {
            return parentId;
        }

        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }
    }
}
